package context

import (
	"sync"

	"gitee.com/dayu1985/framework/logging"

	"github.com/Unknwon/goconfig"
	rice "github.com/giter/go.rice"
)

const (
	SECTION_DATABASE = "database"
)

var mutex = sync.Mutex{}

type Configuration struct {
	INI      *goconfig.ConfigFile
	services map[string]interface{}
}

var config *Configuration

//DefaultConfig default configuration
func DefaultConfig() *Configuration {
	return config
}

func InitConfig(box *rice.Box) *Configuration {

	if config != nil {
		return config
	}

	mutex.Lock()
	defer mutex.Unlock()

	logging.Info("加载默认配置[config-default.ini]... ")

	conf, err := goconfig.LoadFromData(box.MustBytes("config-default.ini"))

	if err != nil {
		panic(err)
	}

	logging.Info("加载系统配置[./conf/config.ini]... ")

	err = conf.AppendFiles("./conf/config.ini")

	if err != nil {
		logging.Warn(err.Error())
	}

	config = &Configuration{
		INI:      conf,
		services: map[string]interface{}{},
	}

	initLogger(config)

	return config
}
